#include <iostream>
#include <string.h>

using namespace std;

bool make_xor(char *nmea_line)
{
//    cout << "nmea_line i: " << nmea_line << endl;
    char xor_chars[] = "0123456789ABCDEF";
    int chcount = 1, len_nmea;
    int Xor = 0;
    len_nmea = strlen(nmea_line);
    while ( (chcount < len_nmea) && ( nmea_line[chcount] != '*') )
        Xor ^= (unsigned short int)(nmea_line[chcount++]);

    if ( nmea_line[chcount] != '*' ) return false;

    nmea_line[chcount + 1] = xor_chars[Xor >> 4];
    nmea_line[chcount + 2] = xor_chars[Xor & 0x0F];

    return true;
}

void    checksum(string &nmeaString)
{
    char *c = (char *)nmeaString.c_str();
    cout << "i : " << c << endl;
    bool b  = make_xor(c);
    if (b)  nmeaString = c;
    cout << "o : " << nmeaString << endl;
}

int main()
{
//    string s = "$PMTK520,0*00cd<x><y>";
//    string s = "$WCESM,012,VARAYOC2,99999,Q,000 00.00N,111 11.11W,87.0,1000,0,-10.0,0.10,100.0,3,60,20120612165730.5*4D<cr><lf>\0";
    string s = "$VAFCS,F,0,VARAYOC,015 05.15S,105 08.45W,0,1e+06,0,0,CT,000080,1,5,000080,1*XX<cr><lf>";
    checksum(s);

    cout << "Hello World!" << endl;
    return 0;
}

